package domain;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import util.Util;
import domain.exceptions.RequiredFieldsException;

@Entity
@Table(name = "users")
public class User extends PersistentEntity{
	@Column(nullable = false)
	private String docnumber;
	
	@Column(nullable = false)
	private String password;
	
	@Column(nullable = false)
	private String name;
	
	@Column(nullable = false)
	private String lastname;
	
	@Column(nullable = false)
	private String email;
	
	//@Column(nullable = false, columnDefinition="enum('M','F')")
	///private String gender;
	
	@Column(nullable = false, columnDefinition = "TINYINT(1)")
	private boolean admin;
	
	@Column(nullable = false, columnDefinition = "TINYINT(1)")
	private boolean confirmed;

	@Column(nullable = false, columnDefinition = "TINYINT(1)")
	private boolean active;

	private Timestamp lastaccess;
	
	@OneToOne(fetch=FetchType.LAZY)
	@Cascade(value = CascadeType.ALL)
	private Profile profile;
	
	//hibernate
	protected User(){
	}

	public User(String username, String name, String lastname, String email) {
		this(username, Util.generatePassword(), name, lastname, email, null, false, true);
	}
	
	public User(String username, String password, String name, String lastname, String email, 
			boolean admin, boolean confirmed) {
		this(username, password, name, lastname, email, null, admin, confirmed);
	}

	public User(String username, String password, String name, String lastname, String email, 
			Timestamp lastaccess, boolean admin, boolean confirmed){
		Timestamp currDate = new Timestamp(Calendar.getInstance().getTime().getTime());
		
		if (username==null || password==null || name==null || lastname==null || email==null) {
			throw new RequiredFieldsException();
		}
		
		if (username.isEmpty() || password.isEmpty() || name.isEmpty() || lastname.isEmpty() ||
				email.isEmpty()) {
			throw new IllegalArgumentException();
		}
		
		if (lastaccess == null) {
			this.lastaccess = currDate;
		} else {
			this.lastaccess = lastaccess;
		}
		
		this.docnumber = username;
		this.password = password;
		this.name = name;
		this.lastname = lastname;
		this.email = email;
		this.admin = admin;
		this.confirmed = confirmed;
		this.active = true;
	}	
	
	public String getPassword() {
		return password;
	}
	
	public String getName() {
		return name;
	}
	public String getLastName() {
		return lastname;
	}

	public String getEmail() {
		return email;
	}
	public String getDocNumber() {
		return docnumber;
	}
	public Date getLastAccess() {
		return lastaccess;
	}
	
	public boolean isAdmin() {
		return admin;
	}
	
	public boolean isConfirmed() {
		return confirmed;
	}
	
	public boolean isActive() {
		return active;
	}
	
	public void setActive(boolean active){
		this.active = active;
	}
	
	public Profile getProfile() {
		return profile;
	}

	public void setLastAccess(Timestamp accessDay) {
		this.lastaccess = accessDay;
	}
	
	public void setPassword(String password) {
		if (password == null) {
			throw new RequiredFieldsException();
		}
		
		if (password.isEmpty()) {
			throw new IllegalArgumentException();
		}
		
		this.password = password;
	}

	public void setName(String name) {
		if (name == null) {
			throw new RequiredFieldsException();
		}
		
		if (name.isEmpty()) {
			throw new IllegalArgumentException();
		}
		
		this.name = name;
	}

	public void setLastName(String lastname) {
		if (lastname == null) {
			throw new RequiredFieldsException();
		}
		
		if(lastname.isEmpty()) {
			throw new IllegalArgumentException();
		}
		
		this.lastname = lastname;
	}

	public void setEmail(String email) {
		if (email == null) {
			throw new RequiredFieldsException();
		}
		
		if (email.isEmpty()) {
			throw new IllegalArgumentException();
		}
		
		this.email = email;
	}

	public void setConfirmed(boolean confirmed) {
		this.confirmed = confirmed;
	}
	
	public void setProfile(Profile profile) {
		this.profile = profile;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result
				+ ((docnumber == null) ? 0 : docnumber.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		if (docnumber == null) {
			if (other.docnumber != null)
				return false;
		} else if (!docnumber.equals(other.docnumber))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "User [username=" + docnumber + ", password=" + password
				+ ", name=" + name + ", lastname=" + lastname + ", email="
				+ email + ", admin=" + admin + ", lastaccess=" + lastaccess
				+ "]";
	}
}